package projects.jnurse.ioc.core;

import projects.jnurse.ioc.core.reflection.Resolver;
import static projects.jnurse.ioc.core.reflection.ResolverFactory.*;
import projects.jnurse.ioc.annotations.Fill;

/**
 * Created by Adil Naji.
 * User: XP-170708
 * Date: 25 nov. 2008
 * Time: 16:13:37
 */
public class InjectionFactory {

    public static Injector getInjector() {
        return new ReflectionInjector();
    }

    public static interface Injector {
        public <T> T getInstance(Class<? extends T> clazz);
        public <T> T fill(T object);
    }

    public static class ReflectionInjector implements Injector {
        public <T> T getInstance(Class<? extends T> clazz) {
            T instance = null;
            try {
                instance = (T)clazz.newInstance();
                scan(Resolver.Type.FIELD).
                        annotated(Fill.class, "by").
                        in(instance);
            } catch(Exception e) { e.printStackTrace(); }
            return instance;
        }

        public <T> T fill(T object) {
            try {
                scan(Resolver.Type.FIELD).
                        annotated(Fill.class, "by").
                        in(object);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return object;
        }
    }

}
